<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增合作伙伴')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-partner-add">
            <input name="partnerId"  type="hidden">
            <input name="deptId"  type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label">类型：</label>
                <div class="col-sm-8">
                    <select id="partnerType" name="partnerType" class="form-control m-b" th:with="type=${@dict.getType('b_partner_type')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">名称：</label>
                <div class="col-sm-8">
                    <input name="name" id="name" class="form-control" type="text" onblur="queryPartner()" required>
                </div>
            </div>
            <div class="form-group"  id="cardNoRow">
                <label class="col-sm-3 control-label is-required">企业税号：</label>
                <div class="col-sm-8">
                    <input name="cardNo" class="form-control" type="text" onblur="checkCardNo()" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">联系人：</label>
                <div class="col-sm-8">
                    <input name="linkName" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">联系电话：</label>
                <div class="col-sm-8">
                    <input name="linkPhone" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">联系地址：</label>
                <div class="col-sm-8">
                    <input name="linkAddress" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <input name="remark" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "partner/partner"
        $("#form-partner-add").validate({
            focusCleanup: true
        });
        //下拉框改变
        document.getElementById("partnerType").onchange = function(e){
            let type = e.currentTarget.value;
            if(type==0){
                $("#cardNoRow").show();
            }else{
                $("#cardNoRow").hide();
            }
        };
        //检查税号唯一性
        function  checkCardNo(){
            let cardNo = $("input[name='cardNo']").val();
            if(cardNo==null || cardNo==""){
                return;
            }
            $.ajax({
                type: "POST",
                url: prefix + "/queryPartnerList",
                data: { "cardNo": cardNo },
                success: function(result) {
                    if(result.code==0 ){
                        debugger;
                        $.modal.alertError("已经存在相同企业税号，请重新输入！");
                    }
                }
            })
        }
        //查询合作伙伴
        function queryPartner(){
            let name = $("#name").val();
            if(name==null || name==""){
                return;
            }
            $.ajax({
                type: "POST",
                url: prefix + "/queryPartnerList",
                data: {
                    "name": name
                },
                success: function(result) {
                    if(result.code==0){
                        $("input[name='cardNo']").val(result.data.cardNo);
                        $("input[name='linkName']").val(result.data.linkName);
                        $("input[name='linkPhone']").val(result.data.linkPhone);
                        $("input[name='linkAddress']").val(result.data.linkAddress);
                        $("input[name='remark']").val(result.data.remark);

                        if(!isEmptyString(result.data.linkPhone)){
                            $("input[name='cardNo']").attr("readonly",true)
                            $("input[name='linkName']").attr("readonly",true)
                            $("input[name='linkPhone']").attr("readonly",true)
                            $("input[name='linkAddress']").attr("readonly",true)
                            $("input[name='remark']").attr("readonly",true)
                        }else{
                            $("input[name='cardNo']").attr("readonly",false)
                            $("input[name='linkName']").attr("readonly",false)
                            $("input[name='linkPhone']").attr("readonly",false)
                            $("input[name='linkAddress']").attr("readonly",false)
                            $("input[name='remark']").attr("readonly",false)
                        }
                    }
                }
            })
        }

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-partner-add').serialize());
            }
        }
    </script>
</body>
</html>
